<template>
    <view class="content">
        <image src="../../static/image/logo.png" class="logo"></image>
        <view class="btn" @click="login">授权登录</view>
    </view>
</template>

<script>
    import {
        ajaxPost,
        isNull,
        getSync,
        getParam,
        setSync
    } from '../../static/js/common.js'
    export default {
        data() {
            return {
                type: 1,
            }
        },
        onLoad() {
            setTimeout(() => {
                this.login();
            }, 300);
        },
        methods: {
            login() {
                const _this = this;
                let url = window.location.href;
                console.log('code', getParam('code'))
                if (getParam('code')) {
                    let code = getParam('code');
                    _this.code(code)
                } else {
                    window.location.href =
                        'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx13f71a65a4c42951&redirect_uri=' +
                        encodeURIComponent(url) + '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
                }
            },

            code(code) {
                ajaxPost('weixin_login', {
                    code: code, //微信授权码
                }).then(res => {
                    console.log('weixin_login', res)
                    if (res.success == 1000) {
                        if (res.detail.user_token) {
                            setSync('userToken', res.detail.user_token); //存token
                            setSync('userInfo', res.detail);
                            uni.showModal({
                                title: '提示',
                                content: '登录成功',
                                showCancel: false,
                                confirmColor: '#40bef1',
                                success: (val) => {
                                    if (val.confirm) {
                                        if (window.location.href.indexOf("?#") < 0) {
                                            window.location.href = 'https://gzh.mzwhcb.cn/h5/?#/';
                                        }
                                        // uni.switchTab({
                                        //     url: "/pages/tabbar/index"
                                        // })
                                    }
                                }
                            });
                        }
                    }
                })
            },
        },
    }
</script>

<style>
    .wximg {
        width: 90rpx;
        height: 90rpx;
    }

    page {
        width: 100vw;
        background-color: #FFFFFF;
        padding: 10vw;
        box-sizing: border-box;

    }

    .content {
        display: flex;
        justify-content: center;
        flex-direction: column;
    }

    .logo {
        width: 253upx;
        height: 154upx;
        margin: 0 auto;
    }

    .btn {
        width: 40vw;
        height: 90upx;
        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
        background-color: #40bef1;
        font-size: 32upx;
        color: #fff;
        border-radius: 30px;
        margin: 100upx auto;
    }
</style>